package com.lhn.bbs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhn.bbs.dto.ActionResult;
import com.lhn.bbs.entity.Class1;
import com.lhn.bbs.entity.User;
import com.lhn.bbs.entity.Userclass;
import com.lhn.bbs.mapper.Class1Dao;
import com.lhn.bbs.mapper.ThemeDao;
import com.lhn.bbs.mapper.UserclassDao;
import com.lhn.bbs.service.Class1Service;
import com.lhn.bbs.vo.ClassByTeacherIdVo;
import com.lhn.bbs.vo.ClassTheme;
import com.lhn.bbs.vo.ThemeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class Class1ServiceImpl implements Class1Service {

    @Autowired(required = false)
    private Class1Dao class1Dao ;

    @Autowired(required = false)
    private ThemeDao themeDao ;

    @Autowired(required=false)
    private UserclassDao userclassDao ;

    /**
     * 添加班级
     * @param class1
     * @return
     */
    @Override
    public ActionResult addClass(Class1 class1) {

        try{
           Boolean b = class1Dao.addClass(class1);
           Integer classid = class1Dao.findClassID(class1);

           log.info("++++++"+classid+"+++++++++"+class1.getTeacharid());
            Userclass userclass = new Userclass();
            userclass.setClassid(classid);
            userclass.setUserid(class1.getTeacharid());
            Boolean b2 = userclassDao.addUserClass(userclass);
            return new ActionResult(200,"添加成功",null, null);

        }catch (Exception e){
            log.info("+++++++++添加班级出错了"+e.getMessage());
            return new ActionResult(201,"添加失败",null,null);
        }
    }

    /**
     * 查询班级主题
     * @param userid
     * @return
     */
    @Override
    public ActionResult findClassTheme(Integer userid, Integer page, Integer limit) {

        List<ClassByTeacherIdVo> classByTeacherIdVos = class1Dao.findClassByTeacherID(userid);

        PageHelper.startPage(page,limit);
        List<ClassTheme> classThemeList = new ArrayList<>();

        for(ClassByTeacherIdVo classByTeacherIdVo:classByTeacherIdVos){
            List<ClassTheme> themeByClass = class1Dao.findThemeByClass(classByTeacherIdVo.getClassid());
            for (ClassTheme theme :themeByClass){
                classThemeList.add(theme);
            }
        }

        for(ClassTheme themeVo:classThemeList){
            int thememun = themeDao.findByIdcount(themeVo.getThemeid());
            themeVo.setThemenum(thememun);
        }
        PageInfo<ClassTheme> pageInfo = new PageInfo<>(classThemeList);
        long total = pageInfo.getTotal();
        return new ActionResult(200,"success" ,pageInfo,total);
    }

    /**
     * 根据老师id 查询创建的班级
     * @param userid
     * @return
     */
    @Override
    public ActionResult findclassByteacherid(Integer userid) {

        List<ClassByTeacherIdVo> classByTeacherIdVos = class1Dao.findClassByTeacherID(userid);

        for(ClassByTeacherIdVo classByTeacherIdVo:classByTeacherIdVos){
            int  classnumber = class1Dao.findByClassIdcount(classByTeacherIdVo.getClassid());

            classByTeacherIdVo.setClassnumber(classnumber);
        }

        return new ActionResult(200,"success" ,classByTeacherIdVos,null);
    }

    /**
     * 根据班级id删除班级
     * @param classid
     * @return
     */
    @Override
    public ActionResult deleteClassById(Integer classid) {
        try{
            Boolean b = class1Dao.deleteClassByClassid(classid);
            Boolean b2 = userclassDao.deleteUserclassByclassid(classid);


            return new ActionResult(200,"success",null ,null );
        }catch (Exception e) {
            log.info("++++"+e.getMessage());
            return new ActionResult(201,"not  success",null ,null );
        }
    }
//   查询班级人员信息
    @Override
    public ActionResult findCLassStudent(Integer classid, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<User>  users = class1Dao.findByIdStudent(classid);

        PageInfo<User> pageInfo = new PageInfo<>(users);
        long total = pageInfo.getTotal();

        return new ActionResult(200,"success" ,pageInfo,total);
    }
//    根据班级id删除班级人员
    @Override
    public ActionResult deleteClassByid(Integer userid,Integer classid) {
        log.info("+++++++++"+userid+"+++"+classid);
       long  lon =  userclassDao.deleteClassByUserid(userid,classid);
        if(lon > 0){
            return new ActionResult(200,"删除成功",null,null);
        }
        return new ActionResult(201,"删除成功",null,null);
    }
//   模糊查询班级
    @Override
    public ActionResult findlikeclass(String likeclassname, Integer page, Integer limit) {

        PageHelper.startPage(page,limit);
        List<ClassByTeacherIdVo>  list =class1Dao.findlikenameClass(likeclassname);

        for(ClassByTeacherIdVo classByTeacherIdVo:list){
            int  classnumber = class1Dao.findByClassIdcount(classByTeacherIdVo.getClassid());

            classByTeacherIdVo.setClassnumber(classnumber);
        }

        PageInfo<ClassByTeacherIdVo> pageInfo=new PageInfo(list);
        long total = pageInfo.getTotal();
        if(list != null){
//            log.info("++++++"+list);
            return new ActionResult(200,"success",pageInfo,total);
        }

        return new ActionResult(201,"查询失败",null,null);
    }
//   加入班级
    @Override
    public ActionResult joinclassByid(Userclass userclass) {
        log.info(userclass.getClassid()+"+++++++"+userclass.getUserid());

        Userclass userclass1 = userclassDao.findByid(userclass);
        if(userclass1 == null) {
            Boolean aBoolean = userclassDao.addUserClass(userclass);

            if (aBoolean) {
                return new ActionResult(200, "success", null, null);
            }else{
                return new ActionResult(202, "加入失败", null, null);
            }
        }
        return new ActionResult(201,"已加入",null ,null);
    }
//   退出班级
    @Override
    public ActionResult studentdeleteclass(Integer classid, Integer userid) {
        try{
            long l = userclassDao.deleteClassByUserid(classid,userid);
                return new ActionResult(200,"success",null ,null);
        }catch (Exception e){
            log.info(e.getMessage());
            return new ActionResult(201,"退出失败",null ,null);
        }
    }

    @Override
    public ActionResult findAllclass(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<ClassByTeacherIdVo> listclass = class1Dao.findAllclass();

        PageInfo<ClassByTeacherIdVo> pageInfo=new PageInfo(listclass);
        long total = pageInfo.getTotal();
        if(listclass != null){
            return new ActionResult(200,"success",pageInfo,total);
        }
        return new ActionResult(201,"查询失败",null,null);
    }
}
